<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */

require_once ROOT_PATH . '/lib/models/argclass/Workshift.php';
require_once ROOT_PATH . '/lib/models/argclass/Workgroup.php';

require_once ROOT_PATH . '/lib/models/time/AttendanceRecord.php';
require_once ROOT_PATH . '/lib/extractor/time/EXTRACTOR_AttendanceRecord.php';

class ArgClassController {

	const INVALID_TIMESHEET_PERIOD_ERROR = "INVALID_TIMESHEET_PERIOD_ERROR";
	const EVENT_OUTSIDE_PERIOD_FAILURE = "EVENT_OUTSIDE_PERIOD_FAILURE";
	const NO_TIMESHEET_FAILURE = "NO_TIMESHEET_FAILURE";
	const ZeroOrNegativeIntervalSpecified_ERROR = "ZeroOrNegativeIntervalSpecified_ERROR";
	const ProjectNotSpecified_ERROR = "ProjectNotSpecified_ERROR";
	const ActivityNotSpecified_ERROR = "ActivityNotSpecified_ERROR";
	const InvalidStartTime_ERROR = "InvalidStartTime_ERROR";
	const InvalidEndTime_ERROR = "InvalidEndTime_ERROR";
	const ReportedDateNotSpecified_ERROR = "ReportedDateNotSpecified_ERROR";
	const InvalidReportedDate_ERROR = "InvalidReportedDate_ERROR";
	const InvalidDuration_ERROR = "InvalidDuration_ERROR";
	const NoValidDurationOrInterval_ERROR = "NoValidDurationOrInterval_ERROR";
	const NotAllowedToSpecifyDurationAndInterval_ERROR = "NotAllowedToSpecifyDurationAndInterval_ERROR";

	private $objTime;
	private $id;
	private $authorizeObj;
	private $db = "";

	public function setObjTime($objTime) {
		$this->objTime=$objTime;
	}

	public function getObjTime() {
		return $this->objTime;
	}

	public function setId($id) {
		$this->id=$id;
	}

	public function getId() {
		return $this->id;
	}

	public function __construct() {
		$this->authorizeObj = new authorize($_SESSION['empID'], $_SESSION['isAdmin']);
		$this->db = new DMLFunctions();
		error_reporting(0);
	}

	public function __distruct() {

	}

	public function viewSelectEmployee() {

		if ($_SESSION['isAdmin'] == 'No' && !$_SESSION['isSupervisor']) {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/time/selectEmployee.php";

		$roles = array(authorize::AUTHORIZE_ROLE_ADMIN, authorize::AUTHORIZE_ROLE_SUPERVISOR);
		$role = $this->authorizeObj->firstRole($roles);

		$employees = null;
		$pendingTimesheets = null;
		$pending=false;
		if ($this->authorizeObj->isSupervisor()) {
			$empRepObj = new EmpRepTo();

			$employees = $empRepObj->getEmpSubDetails($_SESSION['empID']);

			$timesheetObj = new Timesheet();
			$timesheetObj->setStatus(Timesheet::TIMESHEET_STATUS_SUBMITTED);
			for ($i=0; $i<count($employees); $i++) {
				$timesheetObj->setEmployeeId($employees[$i][0]);
				$newTimesheets=$timesheetObj->fetchTimesheets();

				$pendingTimesheets[$employees[$i][0]]=$newTimesheets;
				if (isset($newTimesheets) && $newTimesheets) {
					$pending=true;
				}
			}
		}

		$dataArr[0] = $role;
		$dataArr[1] = $employees;
		$dataArr[2] = $pendingTimesheets;
		$dataArr[3] = $pending;
		$dataArr['empList'] = EmpInfo::getEmployeeMainDetails();

		$template = new TemplateMerger($dataArr, $path);
		$template->display();
	}

	public function defineWorkWeekView() {
		$path = "/templates/time/defineWorkWeek.php";

		$timesheetSubmissionPeriodObj = new TimesheetSubmissionPeriod();

		$timesheetSubmissionPeriodObj->setTimesheetPeriodId(1);
		$tmpArr = $timesheetSubmissionPeriodObj->fetchTimesheetSubmissionPeriods();

		$dataArr[0] = $tmpArr[0];

		$template = new TemplateMerger($dataArr, $path);
		$template->display();
	}

	public function saveWorkWeek() {
		$timesheetSubmissionPeriod = $this->getObjTime();
		$timesheetSubmissionPeriod->setFrequency(TimesheetSubmissionPeriod::TIMESHEET_SUBMISSION_PERIOD_FREQUENCY_WEEK);

		try {
			$res = $timesheetSubmissionPeriod->saveTimesheetSubmissionPeriod();
		} catch (TimesheetSubmissionPeriodException $err) {
			$_GET['message'] = 'EXCEPTION_THROWN_WARNING';

			$this->redirect($_GET['message'], "?timecode=Time&action=Work_Week_Edit_View");
		}

		if ($res) {
			$_GET['message'] = 'UPDATE_SUCCESS';
			Config::setTimePeriodSet('Yes');
		} else {
			$_GET['message'] = 'UPDATE_FAILURE';
		}

		$this->redirect($_GET['message'], "?timecode=Time&action=Work_Week_Edit_View");
	}

	/* Timegrid methods: Begin */

	public function editTimesheetGrid($messageType=null, $message=null, $showComments = 'No') {

		$timesheet = $this->objTime;

		/* Setting Grid array: Begins */
		$timeEventObj = new TimeEvent();
		$timeEventObj->setTimesheetId($timesheet->getTimesheetId());
		$timeEventObj->setEmployeeId($timesheet->getEmployeeId());
		$timeEventObj->setStartTime($timesheet->getStartDate());
		$timeEventObj->setEndTime($timesheet->getEndDate());
		$timeEvents = $timeEventObj->fetchTimeEvents();

		$eventsCount = count($timeEvents);
		$grid = array();
		$activityObj = new ProjectActivity();

		if ($eventsCount > 0) {

			for ($i=0; $i<$eventsCount; $i++) {

				$projectId = $timeEvents[$i]->getProjectId();
				$activityId = $timeEvents[$i]->getActivityId();
				$gridKey = $timeEvents[$i]->getEmployeeId().'-'.$projectId.'-'.$activityId;
				$dateKey = strtotime($timeEvents[$i]->getReportedDate());

				if (!isset($grid[$gridKey])) {
					$activityObj->setId($activityId);
					$activityObj->fetch();
					$projectObj = new Projects();
					$projectObj->setProjectId($projectId);
					$projectObj->fetch();

					$grid[$gridKey]['projectId'] = $projectId; // TODO: Remove this and use project object in the template
					$grid[$gridKey]['projectObj'] = $projectObj;
					$grid[$gridKey]['activityId'] = $activityId; // TODO: Remove this and use activity object in the template
					$grid[$gridKey]['activityName'] = $activityObj->getName();
					$grid[$gridKey]['isActivityDeleted'] = $activityObj->isDeleted();
					$grid[$gridKey]['activityList'] = $activityObj->getActivityList($projectId);

				}

				$grid[$gridKey][$dateKey]['duration'] = round($timeEvents[$i]->getDuration()/3600, 2);
				$grid[$gridKey][$dateKey]['eventId'] = $timeEvents[$i]->getTimeEventId();
				$grid[$gridKey][$dateKey]['comment'] = $timeEvents[$i]->getDescription();

			}

			$records['grid'] = $grid;

		} else {
			$records['grid'] = null;
		}
		/* Setting Grid array: Ends */

		/* Setting Projects List: Begins */
		$projectObj = new Projects();
		$projectObj->setDeleted(Projects::PROJECT_NOT_DELETED);
		$projects = $projectObj->fetchProjects(false);
		$projectsCount = count($projects);

		if ($projectsCount > 0) {

			for ($i=0; $i<$projectsCount; $i++) {

				$projectId = $projects[$i]->getProjectId();
				$projectsList[$i]['name'] = $projects[$i]->retrieveCustomerName($projectId).
				' - '.$projects[$i]->getProjectName();
				$projectsList[$i]['id'] = $projectId;
				$projectsList[$i]['deleted'] = $projects[$i]->getDeleted();

			}

			$records['projectsList'] = $projectsList;

		} else {
			$records['projectsList'] = null;
		}
		/* Setting Projects List: Ends */

		$records['employeeId'] = $timesheet->getEmployeeId();
		$records['timesheetId'] = $timesheet->getTimesheetId();
		$records['timesheetPeriodId'] = $timesheet->getTimesheetPeriodId();
		$records['startDateStamp'] = strtotime($timesheet->getStartDate());
		$records['endDateStamp'] = strtotime($timesheet->getEndDate());
		if (isset($messageType)) {
			$records['messageType'] = $messageType;
			$records['message'] = $message;
		}
		$records['showComments'] = $showComments;

		$path='/templates/time/editTimesheetGrid.php';
		$template = new TemplateMerger($records, $path);
		$template->display();

	}


	/* Timegrid methods: End */

	public function viewDefineEmployeeTimeReport() {
		$path="/templates/time/defineEmployeeTimeReport.php";

		$roles = array(authorize::AUTHORIZE_ROLE_ADMIN, authorize::AUTHORIZE_ROLE_SUPERVISOR);
		$role = $this->authorizeObj->firstRole($roles);

		if (!$role) {
			$this->redirect('UNAUTHORIZED_FAILURE', '?timecode=Time&action=View_Timesheet');
		}

		$employees = null;
		$pendingTimesheets = null;
		$pending=false;
		if ($this->authorizeObj->isSupervisor()) {
			$empRepObj = new EmpRepTo();

			$employees = $empRepObj->getEmpSubDetails($_SESSION['empID']);
			$timesheetObj = new Timesheet();
			$timesheetObj->setStatus(Timesheet::TIMESHEET_STATUS_SUBMITTED);
			for ($i=0; $i<count($employees); $i++) {
				$timesheetObj->setEmployeeId($employees[$i][0]);
				$newTimesheets=$timesheetObj->fetchTimesheets();
				$pendingTimesheets[$employees[$i][0]]=$newTimesheets;
				if (isset($newTimesheets) && $newTimesheets) {
					$pending=true;
				}
			}
		}

		$customerObj = new Customer();
		$projectObj = new Projects();

		$customers = $customerObj->fetchCustomers(0, '', -1 , 1);
		$projectObj->setDeleted(0); // choose only not deleted records
		$projects = $projectObj->fetchProjects();

		$dataArr[0] = $role;
		$dataArr[1] = $employees;
		$dataArr[2] = $projects;

		$dataArr['empList'] = array();
		if ($role == authorize::AUTHORIZE_ROLE_ADMIN) {
			$dataArr['empList'] = EmpInfo::getEmployeeMainDetails();
		} elseif ($role == authorize::AUTHORIZE_ROLE_SUPERVISOR) {
			$dataArr['empList'] = $this->_getSubsForAutoComplete($_SESSION['empID']);
		}

		$template = new TemplateMerger($dataArr, $path);
		$template->display();
	}


	public function viewEmployeeTimeReport() {

		$path="/templates/time/employeeTimeReport.php";

		$timeEventObj = $this->objTime[0];
		$startDate = $this->objTime[1];
		$endDate = $this->objTime[2];

		$roles = array(authorize::AUTHORIZE_ROLE_ADMIN);

		if ($timeEventObj && $this->authorizeObj->isTheSupervisor($timeEventObj->getEmployeeId())) {
			$roles[] = authorize::AUTHORIZE_ROLE_SUPERVISOR;
		}

		$role = $this->authorizeObj->firstRole($roles);

		if (!$role) {
			$this->redirect('UNAUTHORIZED_FAILURE');
		}

		$employeeObj = new EmpInfo();

		$employee = $employeeObj->filterEmpMain($timeEventObj->getEmployeeId());

		$report = $timeEventObj->timeReport($startDate, $endDate);

		$dataArr[0] = $role;
		$dataArr[1] = $employee[0];
		$dataArr[2] = $report;

		$template = new TemplateMerger($dataArr, $path);
		$template->display();
	}

	public function showPrint() {
		$path = "/templates/time/printPop.php";

		$template = new TemplateMerger(null, $path);
		$template->display();
	}

	public function redirect($message=null, $url = null) {

		if (isset($url)) {
			$mes = "";
			if (isset($message)) {
				$mes = "&message=";
			}
			$url=array($url.$mes);
			$id="";
		} else if (isset($message)) {
			preg_replace('/[&|?]+id=[A-Za-z0-9]*/', "", $_SERVER['HTTP_REFERER']);

			if (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0) {
				$message = "&message=".$message;
				$url = preg_split('/(&||\?)message=[A-Za-z0-9]*/', $_SERVER['HTTP_REFERER']);
			} else {
				$message = "?message=".$message;
			}

			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && !is_array($_REQUEST['id'])) {
				$id = "&id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		} else {
			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0)) {
				$id = "&id=".$_REQUEST['id'];
			} else if (preg_match('/&/', $_SERVER['HTTP_REFERER']) == 0){
				$id = "?id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		}

		header("Location: ".$url[0].$message.$id);
	}
	
	//获取班制定义值
	public function getRuleValue($key = "") {
		static $RuleValue = null;
		if ($RuleValue === null) {
			$sql = "select * from hs_hr_sysconfigs where `key`='ClassRule'";
			$query = $this->db->executeQuery($sql);
			$result = mysql_fetch_array($query, MYSQL_ASSOC);
			if($result['value'] != "") {
				$RuleValue = unserialize($result['value']);
			} else {
				$RuleValue = array();
			}
		}
		
		if($key != "") {
			return $RuleValue[$key];
		} else {
			return $RuleValue;
		}
	}
	/**
	 *班制定义显示 
	 **/
	public function showClassRule() {
		$records = $this->getRuleValue();
		//print_r($records);
		//die();
		$path = "/templates/argclass/defineClass.php";
		$template = new TemplateMerger($records, $path);
		$template->display();
	}

	/**
	 *班制定义保存 
	 **/
	public function saveClassRule() {
		
		$value = serialize($_POST);
		//保存数据
		
		$sql = "select * from hs_hr_sysconfigs where `key`='ClassRule'";
		$query = $this->db->executeQuery($sql);
		$records = mysql_fetch_array($query, MYSQL_ASSOC);
		if($records == "") {
			$sql = "insert into  hs_hr_sysconfigs (`key`,`value`,`addtime`) values ('ClassRule','{$value}',".time().")";
			$resulq = $this->db->executeQuery($sql);
		} else {
			$sql = "update hs_hr_sysconfigs set  `value` = '{$value}' where `key`='ClassRule'";
			$resulq = $this->db->executeQuery($sql);
		}
		$this->redirect("保存成功", "?argcode=defineclass");
	}

	public function viewShifts() {

		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/argclass/workShifts.php";

		$objs[] = Workshift::getWorkshifts();
		$objs['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($objs, $path);
		$template->display();
	}

	/**
	 * View the worksheet edit page
	 * @param int $id The workshift Id
	 */
	public function viewEditWorkShift($id) {
		$path = "/templates/argclass/editWorkShift.php";

		try {
			$workshift = Workshift::getWorkshift($id);

			$objs[] = $workshift;
			$objs[] = $workshift->getAssignedEmployees();
			$objs[] = $workshift->getEmployeesWithoutWorkshift();
			$objs['rights'] = $_SESSION['localRights'];

			$template = new TemplateMerger($objs, $path);
			$template->display();

		} catch (WorkshiftException $e) {

			switch ($e->getCode()) {
				case WorkshiftException::WORKSHIFT_NOT_FOUND:
					$msg = 'INVALID_WORK_SHIFT_FAILURE';
					break;
				default:
					$msg = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
			$this->redirect($msg, '?argcode=classlist&action=View_Work_Shifts');
		}
	}

	public function saveWorkShift() {
		$workShift = $this->getObjTime();

		try {
			$res = $workShift->save();
		} catch (WorkshiftException $exception) {
			$this->redirect('INVALID_WORK_SHIFT_FAILURE', '?argcode=argclass&action=View_Work_Shifts');
		}

		if ($res) {
			$this->redirect('UPDATE_SUCCESS', '?argcode=classlist&action=View_Work_Shifts');
		} else {
			$this->redirect('UPDATE_FAILURE', '?argcode=classlist&action=View_Work_Shifts');
		}
	}

	public function updateWorkShift() {

		$obj = $this->getObjTime();
		$workShift = $obj[0];
		$assignedEmployees = $obj[1];
		$id = $workShift->getWorkshiftId();

		try {
			$workShift->save();
			$workShift->removeAssignedEmployees();
			$workShift->assignEmployees($assignedEmployees);

			/* Updating pending leaves accordingly: Begins */

			$empList = $workShift->getAssignedEmployees();

			if (!empty($empList)) {

				foreach ($empList as $emp) {
					$empIdList[] = $emp[Workshift::DB_FIELD_EMP_NUMBER];
				}

				$duration = $workShift->getHoursPerDay();
				$leaveObj = new Leave();

				if (!$leaveObj->adjustLeaveToWorkshift($duration, $empIdList)) {
					throw new Exception('Updating pending leaves failed for new workshift value');
				}

			}

			/* Updating pending leaves accordingly: Ends */

		} catch (WorkshiftException $exception) {
			$this->redirect('UPDATE_FAILURE', '?argcode=classlist&action=View_Edit_Work_Shift&id='.$id);
		}

		$this->redirect('UPDATE_SUCCESS', '?argcode=classlist&action=View_Edit_Work_Shift&id='.$id);
	}

	public function deleteWorkShifts() {
		$workShifts = $this->getObjTime();

		try {
			foreach ($workShifts as $workShift) {
				$res = $workShift->delete();
			}
			$mes = 'DELETE_SUCCESS';
		} catch (WorkshiftException $exception) {
			switch ($exception->getCode()) {
				case WorkshiftException::ERROR_IN_DB_QUERY : // fall through
				case WorkshiftException::INVALID_ROW_COUNT :
					$mes = 'DELETE_FAILURE';
					break;
				case WorkshiftException::INVALID_ID :
					$mes = 'INVALID_ID_FAILURE';
					break;
				default:
					$mes = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
		}

		$this->redirect($mes, '?argcode=classlist&action=View_Work_Shifts');
	}

	/**
	 * view the work groups
	 * @author johnson  2012/03/14
	 */
	public function viewGroups() {

		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/argclass/workGroups.php";

		$objs[] = Workgroup::getWorkgroups();
		$objs['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($objs, $path);
		$template->display();
	}

	/**
	 * View the worksheet edit page
	 * @param int $id The workgroup Id
	 */
	public function viewEditWorkGroup($id) {
		$path = "/templates/argclass/editWorkGroup.php";

		try {
			$workgroup = Workgroup::getWorkgroup($id);

			$objs[] = $workgroup;
			$objs[] = $workgroup->getAssignedEmployees();
			$objs[] = $workgroup->getEmployeesWithoutWorkgroup();
			$objs['rights'] = $_SESSION['localRights'];

			$template = new TemplateMerger($objs, $path);
			$template->display();

		} catch (WorkgroupException $e) {

			switch ($e->getCode()) {
				case WorkgroupException::WORKGROUP_NOT_FOUND:
					$msg = 'INVALID_WORK_SHIFT_FAILURE';
					break;
				default:
					$msg = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
			$this->redirect($msg, '?argcode=workgroup&action=View_Work_Groups');
		}
	}

	public function saveWorkGroup() {
		$workGroup = $this->getObjTime();

		try {
			$res = $workGroup->save();
		} catch (WorkgroupException $exception) {
			$this->redirect('INVALID_WORK_SHIFT_FAILURE', '?argcode=workgroup&action=View_Work_Groups');
		}

		if ($res) {
			$this->redirect('UPDATE_SUCCESS', '?argcode=workgroup&action=View_Work_Groups');
		} else {
			$this->redirect('UPDATE_FAILURE', '?argcode=workgroup&action=View_Work_Groups');
		}
	}

	public function updateWorkGroup() {

		$obj = $this->getObjTime();
		$workGroup = $obj[0];
		$assignedEmployees = $obj[1];
		$id = $workGroup->getWorkgroupId();

		try {
			$workGroup->save();
			$workGroup->removeAssignedEmployees();
			$workGroup->assignEmployees($assignedEmployees);

			/* Updating pending leaves accordingly: Begins */

			$empList = $workGroup->getAssignedEmployees();

			if (!empty($empList)) {

				foreach ($empList as $emp) {
					$empIdList[] = $emp[Workgroup::DB_FIELD_EMP_NUMBER];
				}

				/*$duration = $workGroup->getHoursPerDay();
				$leaveObj = new Leave();

				if (!$leaveObj->adjustLeaveToWorkshift($duration, $empIdList)) {
				throw new Exception('Updating pending leaves failed for new workgroup value');
				}*/

			}

			/* Updating pending leaves accordingly: Ends */

		} catch (WorkgroupException $exception) {
			$this->redirect('UPDATE_FAILURE', '?argcode=workgroup&action=View_Edit_Work_Group&id='.$id);
		}

		$this->redirect('UPDATE_SUCCESS', '?argcode=workgroup&action=View_Edit_Work_Group&id='.$id);
	}

	public function deleteWorkGroups() {
		$workGroups = $this->getObjTime();

		try {
			foreach ($workGroups as $workGroup) {
				$res = $workGroup->delete();
			}
			$mes = 'DELETE_SUCCESS';
		} catch (WorkgroupException $exception) {
			switch ($exception->getCode()) {
				case WorkgroupException::ERROR_IN_DB_QUERY : // fall through
				case WorkgroupException::INVALID_ROW_COUNT :
					$mes = 'DELETE_FAILURE';
					break;
				case WorkgroupException::INVALID_ID :
					$mes = 'INVALID_ID_FAILURE';
					break;
				default:
					$mes = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
		}

		$this->redirect($mes, '?argcode=workgroup&action=View_Work_Groups');
	}


	/**
	 * view the work class
	 * @author johnson  2012/03/19
	 */
	public function viewClassArg() {
		$defaultmonth = @date("m",time())+1;
		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/argclass/argClass.php";

		$year = isset($_POST['year']) ? intval($_POST['year']) : @date("Y",time());
		$list = array();
		$hasDefRecord=false;
		$sql = "select * from hs_hr_argclass_year where c_year='$year'";
		$query = $this->db->executeQuery($sql);
		while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
			//$group = trim($row['c_group'],'班');
			$workgroup = $row['workgroup'];
			$group = $row['c_group'];
			$month = $row['c_month'];
			if($month==$defaultmonth) $hasDefRecord=true;
			$day = $row['c_day'];
			$list[$month][$workgroup][$group][$day] = $row['c_class'];
		}
		//$objs[] = ArgClass::getClassArgs();
		//$objs[] = Workshift::getWorkshifts();
		ksort($list);
		//print_r($list);exit;
		$records['list'] = $list;
		$records['year'] = $year;
		$records['hasDefRecord'] = $hasDefRecord;
		$records['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($records, $path);
		$template->display();
	}

	/**
	 * view the work class of the members
	 * @author johnson  2012/04/27
	 */
	public function batchAddClassMembers() {

		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}
		
		$path = "/templates/argclass/classMembers.php";
		
		$year = isset($_POST['year']) ? intval($_POST['year']) : @date("Y",time());
		$month = isset($_POST['month']) ? intval($_POST['month']) : @date("n",time());

		$empsql = "select a.emp_number,a.group_id,b.name,b.group_object,b.group_typenum,c.emp_firstname from hs_hr_employee_workgroup as a " .
				" left join hs_hr_workgroup as b on a.group_id=b.group_id " .
				" left join hs_hr_employee as c on a.emp_number=c.emp_number " .
				//" where b.group_object=1 or b.group_object=3 or b.group_object=4" .
				" order by a.group_id asc";
		$empquery = $this->db->executeQuery($empsql);
		while ($row = mysql_fetch_array($empquery, MYSQL_ASSOC)) {
			$mbid = $row['emp_number'];
			$list[$mbid]['name'] = $row['c_class'];
			//$name = $row['emp_lastname']." ".$row['emp_firstname'];
			$name = $row['emp_firstname'];
			$group_name = $row['name'];
			$group_id = $row['group_id'];
			$group_type = $row['group_object'];
			$typenum = $row['group_typenum'];
			
			if($group_type==5) { //空白班就是对员工排空白班
				$val_arr=array();
			}else {
				$sql = "select c_day,c_class from hs_hr_argclass_year where c_year='$year' and c_month='$month' and workgroup='$group_type' and c_group='$typenum'";
				$query = $this->db->executeQuery($sql);
				while ($row2 = mysql_fetch_array($query, MYSQL_ASSOC)) {
					$day = $row2['c_day'];
					$list[$mbid]['m_class'][$day] = $row2['c_class'];
					$val_arr[$day] = $row2['c_class'];
				}
			}
			if(!empty($list[$mbid])) {
				$list[$mbid]['group_name'] = $group_name;
				$list[$mbid]['member_name'] = $name;
				$list[$mbid]['mbid'] = $mbid;
				$list[$mbid]['group_type'] = $group_type;
			}
			$val = serialize($val_arr);
			//insert record into hs_hr_argclass_members  johnson  2012/04/27
			$selsql = "select id from hs_hr_argclass_members where mbid='$mbid' and m_year='$year' and m_month='$month'";
			$setquery = $this->db->executeQuery($selsql);
			$info = mysql_fetch_row($setquery);
			if(!$info[0]) {
				$exesql = "insert into hs_hr_argclass_members(mbid,member_name,group_id,group_object,m_group,m_year,m_month,m_class) values('$mbid','$name','$group_id','$group_type','$typenum','$year','$month','$val')";
			}else {
				$exesql = "update hs_hr_argclass_members set group_id='$group_id',group_object='$group_type',m_group='$typenum',m_class='$val' where mbid='$mbid' and m_year='$year' and m_month='$month'";
			}
			$this->db->executeQuery($exesql);
		}
		
		//$objs[] = ArgClass::getClassArgs();
		//$objs[] = Workshift::getWorkshifts();
		$records['list'] = $list;
		$records['year'] = $year;
		$records['month'] = $month;
		$records['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($records, $path);
		$template->display();
	}

	/**
	 * view the work class of the members
	 * @author johnson  2012/04/18
	 */
	public function viewClassMembers() {

		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/argclass/classMembers.php";
		
		$year = isset($_POST['year']) ? intval($_POST['year']) : @date("Y",time());
		$month = isset($_POST['month']) ? intval($_POST['month']) : @date("n",time());
		$employee_id = isset($_POST['employee']) ? intval($_POST['employee']) : '';
		$selempstr = $employee_id ? " and a.mbid='$employee_id'" : "";
		
		$sysConst = new sysConf();
		//$where .= $this->GetSearchSlqWhere();
        $limit = "";
        $list = array();
		$pageNO = isset($_POST['pageNO'])?intval($_POST['pageNO']):1;
        if ($pageNO > 0) {
            $pageNO--;
            $pageNO *= $sysConst->itemsPerPage;

            $limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
        }
        $sql = "select count(*) as allcount from hs_hr_argclass_members a where a.m_year='$year' and a.m_month='$month' ".$selempstr ;
		$query = $this->db->executeQuery($sql);
		$Allcount = mysql_fetch_array($query, MYSQL_ASSOC);
		
		$selsql = "select a.*,b.name as group_name from hs_hr_argclass_members a left join hs_hr_workgroup b on a.group_id=b.group_id where a.m_year='$year' and a.m_month='$month' ".$selempstr." order by a.group_object asc, a.m_group asc {$limit}";
		$empquery = $this->db->executeQuery($selsql);
		while ($row = mysql_fetch_array($empquery, MYSQL_ASSOC)) {
			$mbid = $row['mbid'];
			$list[$mbid] = $row;
			$m_class = unserialize($row['m_class']);
			$list[$mbid]['m_class'] = $m_class;
		}
		//print_r($list);exit;
		$employees = $this->getAllWorkEmployees();
		
		//$objs[] = ArgClass::getClassArgs();
		//$objs[] = Workshift::getWorkshifts();
		$records['list'] = $list;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount['allcount'];
		$records['employees'] = $employees;
		$records['year'] = $year;
		$records['month'] = $month;
		$records['employee_id'] = $employee_id;
		$records['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($records, $path);
		$template->display();
	}
	
	/**
	 * get all the work employees
	 * @author johnson  2012/5/5
	 */
	function getAllWorkEmployees() {
		$empsql = "select a.emp_number,c.emp_firstname from hs_hr_employee_workgroup as a " .
				" left join hs_hr_workgroup as b on a.group_id=b.group_id " .
				" left join hs_hr_employee as c on a.emp_number=c.emp_number " .
				" where b.group_object in ('1','3','4')" .
				" order by a.group_id asc";
		$empquery = $this->db->executeQuery($empsql);
		$employees = array();
		$result = array();
		while ($row = mysql_fetch_array($empquery, MYSQL_ASSOC)) {
			$result['id'] = $row['emp_number'];
			$result['name'] = $row['emp_firstname'];
			$employees[] = $result;
		}
		return $employees;
	}
	
	/**
	 * view the work class's adjust info of the members
	 * @author johnson  2012/04/18
	 */
	public function viewClassAdjust() {

		if ($_SESSION['isAdmin'] == 'No') {
// 			die('You are not authorized to view this page');
		}

		$path = "/templates/argclass/viewClassAdjust.php";
		
		$year = isset($_POST['year']) ? intval($_POST['year']) : @date("Y",time());
		$employee_id = isset($_POST['employee']) ? intval($_POST['employee']) : '';
		$selempstr = $employee_id ? " and mbid='$employee_id'" : "";
		
		$sysConst = new sysConf();
		//$where .= $this->GetSearchSlqWhere();
        $limit = "";
        $list = array();
		$pageNO = isset($_POST['pageNO'])?intval($_POST['pageNO']):1;
        if ($pageNO > 0) {
            $pageNO--;
            $pageNO *= $sysConst->itemsPerPage;

            $limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
        }
        $sql = "select count(*) as allcount from hs_hr_class_adjust where a_year='$year' ".$selempstr." order by class_name asc, handle_time desc;";
		$query = $this->db->executeQuery($sql);
		$Allcount = mysql_fetch_array($query, MYSQL_ASSOC);
		
		$selsql = "select * from hs_hr_class_adjust where a_year='$year' ".$selempstr." order by class_name asc, handle_time desc {$limit}";
		$adjquery = $this->db->executeQuery($selsql);
		while ($row = mysql_fetch_array($adjquery, MYSQL_ASSOC)) {
			$row['class_date'] = $row['a_year']."-".$row['a_month']."-".$row['a_day'];
			$row['adjust_content'] = '将原班次"'.$row['c_before'].'"调整为班次"'.$row['c_now'].'". '.$row['adjust_content'];
			$list[] = $row;
		}
		//print_r($list);exit;
		$employees = $this->getAllWorkEmployees();
		
		//$objs[] = ArgClass::getClassArgs();
		//$objs[] = Workshift::getWorkshifts();
		$records['list'] = $list;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount['allcount'];
		$records['employees'] = $employees;
		$records['year'] = $year;
		$records['employee_id'] = $employee_id;
		$records['rights'] = $_SESSION['localRights'];

		$template = new TemplateMerger($records, $path);
		$template->display();
	}

	/**
	 * View the worksheet edit page
	 * @param int $id The workgroup Id
	 */
	public function viewEditClassArg($id) {
		$path = "/templates/argclass/editClassArg.php";

		try {
			//$argclass = ArgClass::getClassArg($id);

			//$objs[] = $argclass;
			//$objs[] = $argclass->getAssignedEmployees();
			//$objs[] = $argclass->getEmployeesWithoutWorkgroup();
			$objs['rights'] = $_SESSION['localRights'];

			$template = new TemplateMerger($objs, $path);
			$template->display();

		} catch (ArgClassException $e) {

			switch ($e->getCode()) {
				case ArgClassException::WORKSHIFT_NOT_FOUND:
					$msg = 'INVALID_WORK_SHIFT_FAILURE';
					break;
				default:
					$msg = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
			$this->redirect($msg, '?argcode=argclass&action=View_Class_Arg');
		}
	}

	public function saveClassArg() {
		$argclass = $this->getObjTime();

		try {
			$res = $argclass->save();
		} catch (ArgClassException $exception) {
			$this->redirect('INVALID_WORK_SHIFT_FAILURE', '?argcode=argclass&action=View_Class_Arg');
		}

		if ($res) {
			$this->redirect('UPDATE_SUCCESS', '?argcode=argclass&action=View_Class_Arg');
		} else {
			$this->redirect('UPDATE_FAILURE', '?argcode=argclass&action=View_Class_Arg');
		}
	}

	public function updateClassArg() {

		$obj = $this->getObjTime();
		$argClass = $obj[0];
		$assignedEmployees = $obj[1];
		$id = $argClass->getClassArgId();

		try {
			$argClass->save();

		} catch (ArgClassException $exception) {
			$this->redirect('UPDATE_FAILURE', '?argcode=argclass&action=View_Edit_Class_Arg&id='.$id);
		}

		$this->redirect('UPDATE_SUCCESS', '?argcode=argclass&action=View_Edit_Class_Arg&id='.$id);
	}

	public function deleteClassArgs() {
		$argClasses = $this->getObjTime();

		try {
			foreach ($argClasses as $argClass) {
				$res = $argClass->delete();
			}
			$mes = 'DELETE_SUCCESS';
		} catch (ArgClassException $exception) {
			switch ($exception->getCode()) {
				case ArgClassException::ERROR_IN_DB_QUERY : // fall through
				case ArgClassException::INVALID_ROW_COUNT :
					$mes = 'DELETE_FAILURE';
					break;
				case ArgClassException::INVALID_ID :
					$mes = 'INVALID_ID_FAILURE';
					break;
				default:
					$mes = 'UNKNOWN_ERROR_FAILURE';
					break;
			}
		}

		$this->redirect($mes, '?argcode=argclass&action=View_Class_Arg');
	}
	
	/**
	 * 插入各班的起始班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertStartClass($year,$month,$startclass,$startnum,$workgroup,$group,$holiday_days) {
		$i=1;
		for($start=1;$i<=$startnum;$start++) {
			if(in_array($start,$holiday_days)) continue;
			//if($startclass=="WA" || $startclass=="WB" || $startclass=="WC") $startclass = "W";
			if(stripos($startclass,'W')===0) $startclass = "W";
			$stsql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$start','$workgroup','$group','$startclass');";
			//echo $stsql;exit;
			$this->db->executeQuery($stsql);
			$i++;
		}
		return $start;
	}

	/**
	 * 按ABCW的班次链插入该月的班次
	 * @param $year $month $startclass $nowday $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassABC($year,$month,$startclass,$startday,$workgroup,$group,$holiday_days) {
		if(in_array($month,array(1,3,5,7,8,10,12))) $max_day = 31;
		elseif(in_array($month,array(4,6,9,11))) $max_day = 30;
		elseif( $year%4==0 && ($year%100!=0 || $year%400==0) ) $max_day = 29; //闰年二月份29天
		else $max_day = 28;
		$maxd = $max_day+1;
		$i = $startday;
		switch($workgroup) {
			case 3:
				$a="I"; $b="J"; $c="K"; $wa="WI"; $wb="WJ"; $wc="WK";
				break;
			case 4:
				$a="O"; $b="P"; $c="Q"; $wa="WO"; $wb="WP"; $wc="WQ";
				break;
			default:
				$a="A"; $b="B"; $c="C"; $wa="WA"; $wb="WB"; $wc="WC";
				break;
		}
		switch ($startclass) {
			case $a :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $b :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $c :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wa :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wb :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wc :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
		}
	}

	/**
	 * 按BACW的班次链插入该月的班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassBAC($year,$month,$startclass,$startnum,$group) {
		switch ($startclass) {
			case "A" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "B" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "C" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WA" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WB" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WC" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
		}
	}
	
	/**
	 * 按ACBW的班次链插入该月的班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassACB($year,$month,$startclass,$startnum,$group) {
		switch ($startclass) {
			case "A" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "B" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "C" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WA" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WB" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WC" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
		}
	}
	
	/**
	 * 按CABW的班次链插入该月的班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassCAB($year,$month,$startclass,$startnum,$group) {
		switch ($startclass) {
			case "A" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "B" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "C" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WA" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WB" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WC" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
		}
	}
	
	/**
	 * 按BCAW的班次链插入该月的班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassBCA($year,$month,$startclass,$startnum,$group) {
		switch ($startclass) {
			case "A" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "B" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "C" :
				for($d=$startnum+1;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+5) || $d%12==($startnum+9)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}elseif($d%12==($startnum+2) || $d%12==($startnum+3) || $d%12==($startnum+4)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+6) || $d%12==($startnum+7) || $d%12==($startnum+8)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WA" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WB" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
			case "WC" :
				for($d=2;$d<32;$d++) {
					if($d%12==($startnum+1) || $d%12==($startnum+2) || $d%12==($startnum+3)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','C');";
					}elseif($d%12==($startnum+5) || $d%12==($startnum+6) || $d%12==($startnum+7)) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','A');";
					}elseif($d%12==($startnum+9)%12 || $d%12==($startnum+10)%12 || $d%12==($startnum+11)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','B');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,c_group,c_class) values('$year','$month','$d','$group','W');";
					}
					$this->db->executeQuery($inssql);
				}
				break;
		}
	}
	
	/**
	 * 按CBAW的班次链插入该月的班次
	 * @param $year $month $startclass $startnum $group
	 * @return null
	 * @author johnson  2012/4/10
	 **/
	private function insertClassCBA($year,$month,$startclass,$startday,$workgroup,$group,$holiday_days) {
		if(in_array($month,array(1,3,5,7,8,10,12))) $max_day = 31;
		elseif(in_array($month,array(4,6,9,11))) $max_day = 30;
		elseif( $year%4==0 && ($year%100!=0 || $year%400==0) ) $max_day = 29; //闰年二月份29天
		else $max_day = 28;
		$maxd = $max_day+1;
		$i = $startday;
		switch($workgroup) {
			case 3:
				$a="I"; $b="J"; $c="K"; $wa="WI"; $wb="WJ"; $wc="WK";
				break;
			case 4:
				$a="O"; $b="P"; $c="Q"; $wa="WO"; $wb="WP"; $wc="WQ";
				break;
			default:
				$a="A"; $b="B"; $c="C"; $wa="WA"; $wb="WB"; $wc="WC";
				break;
		}
		switch ($startclass) {
			case $a :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $b :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $c :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+4)%12 || $i%12==($startday+8)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}elseif($i%12==($startday+1)%12 || $i%12==($startday+2)%12 || $i%12==($startday+3)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+5)%12 || $i%12==($startday+6)%12 || $i%12==($startday+7)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wa :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wb :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$s');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
			case $wc :
				for($d=$startday;$d<$maxd;$d++) {
					if(in_array($d,$holiday_days)) continue;
					if($i%12==$startday%12 || $i%12==($startday+1)%12 || $i%12==($startday+2)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$c');";
					}elseif($i%12==($startday+4)%12 || $i%12==($startday+5)%12 || $i%12==($startday+6)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$b');";
					}elseif($i%12==($startday+8)%12 || $i%12==($startday+9)%12 || $i%12==($startday+10)%12) {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','$a');";
					}else {
						$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$d','$workgroup','$group','W');";
					}
					$this->db->executeQuery($inssql);
					$i++;
				}
				break;
		}
	}
	
	/**
	 * 行政班年工作安排 
	 **/
	private function ArgOfficeClass($year,$month,$holiday_days) {
		if(in_array($month,array(1,3,5,7,8,10,12))) $max_day = 31;
		elseif(in_array($month,array(4,6,9,11))) $max_day = 30;
		elseif( $year%4==0 && ($year%100!=0 || $year%400==0) ) $max_day = 29; //闰年二月份29天
		else $max_day = 28;
		$maxd = $max_day+1;
		for($i=1;$i<$maxd;$i++) {
			if($month<10) $day = $year."-0".$month;
			else $day = $year."-".$month;
			if($i<10) $day .= "-0".$i;
			else $day .= "-".$i;
			if(@date('w',strtotime($day))==6 || @date('w',strtotime($day))==0) {
				$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$i',2,1,'W'),('$year','$month','$i',2,2,'W');";
			}else{
				$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$i',2,1,'D'),('$year','$month','$i',2,2,'N');";
			}
			$this->db->executeQuery($inssql);
		}
		foreach($holiday_days as $day) {
			$updsqsl = "update hs_hr_argclass_year set c_class='H' where c_year='$year' and c_month='$month' and c_day='$day' and workgroup=2;";
			$this->db->executeQuery($updsqsl);
		}
		$this->redirect("保存成功", "?argcode=argclass&action=View_Class_Arg");
	}
	
	/**
	 * 批量安排工作 
	 **/
	public function saveArgClassBatch() {
		$year = intval($_POST['year']);
		$month = intval($_POST['month']);
		$workgroup = intval($_POST['workgroup']);
		$delsql = "delete from hs_hr_argclass_year where `c_year`='$year' and c_month='$month' and workgroup='$workgroup'";
		$this->db->executeQuery($delsql);
		$month2 = $month<10 ? "0".$month : $month;
		$datekey = $year."-".$month2;
		
		$holidaysql = "select holiday_id,description,date from hs_hr_holidays where (date like '".$datekey."%') or (date like '%-".$month2."-%' and recurring=1)";
		$holidayquery = $this->db->executeQuery($holidaysql);
		$holidays = array();
		$holiday_days = array();
		
		while ($row = mysql_fetch_array($holidayquery, MYSQL_ASSOC)) {
			$holidays[] = $row['date'];
			$day = intval(substr($row['date'],8,2));
			$holiday_days[] = $day;
			if($workgroup!=2) {
				$inssql = "insert into hs_hr_argclass_year(c_year,c_month,c_day,workgroup,c_group,c_class) values('$year','$month','$day','$workgroup','1','H')
						,('$year','$month','$day','$workgroup','2','H')
						,('$year','$month','$day','$workgroup','3','H')
						,('$year','$month','$day','$workgroup','4','H');";
				$this->db->executeQuery($inssql);
			}
		}
		if($workgroup==2) $this->ArgOfficeClass($year,$month,$holiday_days);

		//保存数据
		for ($i=1;$i<5;$i++) {
			$classlink = 'classlink'.$workgroup;
			$class = 'startclass'.$workgroup.$i;
			$startclass = strtoupper($_POST[$class]);
			$num = 'startnum'.$workgroup.$i;
			if($startclass=="W") $startnum==1;
			else $startnum = intval($_POST[$num])>3 ? 3 : intval($_POST[$num]);
			$group = $i;
			$nowday = $this->insertStartClass($year,$month,$startclass,$startnum,$workgroup,$group,$holiday_days);

			switch ($_POST[$classlink]) {
				case "ABC" :
				case "IJK" :
				case "OPQ" :
					$this->insertClassABC($year,$month,$startclass,$nowday,$workgroup,$group,$holiday_days);
					break;
				/*case "BAC" :
					$this->insertClassBAC($year,$month,$startclass,$startnum,$group);
					break;
				case "ACB" :
					$this->insertClassACB($year,$month,$startclass,$startnum,$group);
					break;
				case "CAB" :
					$this->insertClassCAB($year,$month,$startclass,$startnum,$group);
					break;
				case "BCA" :
					$this->insertClassBCA($year,$month,$startclass,$startnum,$group);
					break;*/
				case "CBA" :
				case "KJI" :
				case "QPO" :
					$this->insertClassCBA($year,$month,$startclass,$nowday,$workgroup,$group,$holiday_days);
					break;
			}
		}
		$this->redirect("保存成功", "?argcode=argclass&action=View_Class_Arg");
	}
	
	/**
	 *获取系统的用户名和ID
	 **/
	public function GetUserName() {
		
		$UserName = $_POST['NameVal'];
		$tbname = $_POST['tbname'];
		$NameInput = $_POST['UserNameInput'];
		$div = $_POST['div'];
		$IdInput = $_POST['UserIdInput'];
		$sql = "select user_name from hs_hr_users  where user_name like '%{$UserName}%'";
		//echo $sql;
		//die();
		$query = $this->db->executeQuery($sql);
		
		$str ='<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="$(\'#'.$_POST["div"].'\').hide();">关闭</td></tr>';
        
		while($records = mysql_fetch_array($query, MYSQL_ASSOC))
	    {
	    	$records["user_name2"] = str_replace($UserName,'<font color=red>'.$UserName.'</font>',$records["user_name"]);
	    	$str .= '<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="showproname(\''.$records["user_name"].'\',\''.$records["user_name"].'\',\''.$IdInput.'\',\''.$NameInput.'\',\''.$div.'\');">'.$records["user_name2"].'</td></tr>';
	    }
	    if ($str != "")
	    {
	    	echo $str;
	    }
	    else {
	    	echo "no";
	    }
	    exit;
	}
	
	/**
	 *获取系统的定制的班次
	 **/
	public function GetDefinedClasses() {
		
		$tbname = $_POST['tbname'];
		$div = $_POST['div'];
		$tdcell = $_POST['cell'];
		$params = unserialize(stripslashes($_POST['params']));
		//$sql = "select value from hs_hr_sysconfigs where `key`='ClassRule' limit 1;";
		//echo $sql;
		//die();
		//$query = $this->db->executeQuery($sql);
		//$records = mysql_fetch_array($query, MYSQL_ASSOC);
		$classInfos = array(""=>"不排班","A"=>"A班制","B"=>"B班制","C"=>"C班制","D"=>"行政日班制(D)","N"=>"行政夜班制(N)","H"=>"国家假期(H)","W"=>"周末(W)","M"=>"补充休假(M)","I"=>"保安I班","J"=>"保安J班","K"=>"保安K班","O"=>"厨房O班","P"=>"厨房P班","Q"=>"厨房Q班");
		
		$str ='<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="$(\'#'.$_POST["div"].'\').hide();">关闭</td></tr>';
        
		foreach ($classInfos as $k=>$v) {
			$str .= '<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="handle_request(\''.$tdcell.'\',\''.$k.'\','.$params[mid].','.$params[year].','.$params[month].','.$params[day].',\''.$params[group].'\',\''.$params[gtype].'\')">'.$v.'</td></tr>';
		}
	    if ($str != "")
	    {
	    	echo $str;
	    }
	    else {
	    	echo "no";
	    }
	    exit;
	}
}
?>